package customerEnter.tool;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import customerEnter.Frame.JDBCDao.JdbcTemplate.DynamicDataSourceHolder;
import customerEnter.Frame.Utils.Log4j;

/**
 * 獲取bean
 * 
 * 类名称：springBeans 类描述： 创建人：dy 创建时间：2016-6-3 下午4:42:11
 * 
 */
@Service
public class springBeans implements BeanFactoryAware {

	private static ApplicationContext				ctx;

	private static JdbcTemplate						jdbcTemplate;

	private static BeanFactory						beanFactory;

	// private static StringRedisTemplate redisTemplate;
	private static RedisTemplate<String, String>	redisTemplate;

	/**
	 * 注入bean 工廠
	 */
	@Override
	public void setBeanFactory(BeanFactory factory) throws BeansException {
		beanFactory = factory;
	}

	/**
	 * 測試的bean
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getTestBean(String SpringBeanName) {

		if (WhetherToEmpty.ObjectWhetherToEmpty(ctx)) {
			ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		}
		return (T) ctx.getBean(SpringBeanName);
	}

	public static JdbcTemplate getJdbcTemplate() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(jdbcTemplate)) {
			try {
				jdbcTemplate = getBean("jdbcTemplate");
				if (WhetherToEmpty.ObjectWhetherToEmpty(jdbcTemplate)) {
					DynamicDataSourceHolder.setDataSource("dataSource1");
				}
			} catch (Exception e) {
				Log4j.Log4JOutError("獲取jdbcTemplate失敗：" + e.getMessage());
			}
		}
		return jdbcTemplate;
	}

	public static RedisTemplate<String, String> getRedisTemplate() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(redisTemplate)) {
			try {
				redisTemplate = getBean("redisTemplate");
			} catch (Exception e) {
				Log4j.Log4JOutError(e.getMessage() + " 獲取失敗springbeans");
			}
		}
		return redisTemplate;
	}

	public static void setRedisTemplate(RedisTemplate<String, String> redisTemplates) {
		redisTemplate = redisTemplates;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBeanFactoryBean(String beanName) {
		return (T) beanFactory.getBean(beanName);
	}

	/**
	 * 運行中的bean 用配置文件是獲取不到的
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName) {
		T t = null;
		try {
			t = (T) ContextLoaderListener.getCurrentWebApplicationContext().getBean(beanName);
		} catch (Exception e) {
			Log4j.Log4jOutInfo("獲取bean 失敗:" + e.getMessage());
		}
		return t;
	}

	public static Map<String, String> getBeanNames() {
		WebApplicationContext webApplicationContext = ContextLoaderListener.getCurrentWebApplicationContext();

		String[] str = webApplicationContext.getBeanDefinitionNames();
		Map<String, String> map = new HashMap<String, String>();
		for (String string : str) {
			map.put(string, string);
		}
		return map;
	}

	public static void SetJdbcTemplate(JdbcTemplate jdbcTemplateTest) {
		jdbcTemplate = jdbcTemplateTest;
	}

	public static void SetRedisTemplate(StringRedisTemplate redis) {
		redisTemplate = redis;
	}

}